package cn.xutongxue.gtms.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.xutongxue.gtms.base.Errors;
import cn.xutongxue.gtms.base.Result;
import cn.xutongxue.gtms.constant.UserConstant;
import cn.xutongxue.gtms.entity.File;
import cn.xutongxue.gtms.entity.dto.GuideDto;
import cn.xutongxue.gtms.entity.vo.ManagerVo;
import cn.xutongxue.gtms.entity.vo.TeacherGuideVo;
import cn.xutongxue.gtms.exception.ThrowUtils;
import cn.xutongxue.gtms.service.impl.FileServiceImpl;
import cn.xutongxue.gtms.service.impl.FileUploadServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/guide")
@RestController
@AllArgsConstructor
@CrossOrigin
public class GuideController {

    final private FileServiceImpl fileService;
    final private FileUploadServiceImpl fileUploadService;
    @GetMapping("/getGuideList")
    @SaCheckRole(UserConstant.DEFAULT_ROLE)
    public Result getGuideList() {
        List<ManagerVo> managerVos = fileService.getGuideList();
        return Result.success(managerVos);
    }

    @GetMapping("/getFileList")
    @SaCheckLogin
    public Result getFileList() {
        List<File> managerVos = fileService.getFileList();
        return Result.success(managerVos);
    }

    @PostMapping("/materialSubmit")
    @SaCheckRole(UserConstant.DEFAULT_ROLE)
    public Result materialSubmit(@RequestBody GuideDto guideDto) {
        if (guideDto == null) {
            return Result.fail(Errors.PARAMS_ERROR);
        }
        Long fileId = guideDto.getFileId();
        String fileType = guideDto.getFileType();
        if (fileId == null || fileType == null) {
            return Result.fail(Errors.PARAMS_ERROR);
        }

        File file = fileService.getById(fileId);
        file.setType(fileType);
        fileService.updateById(file);

        return Result.success(true);
    }

    @Transactional
    @GetMapping("/deleteMaterial")
    @SaCheckRole(UserConstant.DEFAULT_ROLE)
    public Result deleteMaterial(@RequestParam("id") Long fileId) {
        // 删除本地资源文件
        Boolean res = fileUploadService.removeFile(fileId);
        ThrowUtils.throwIf(!res, Errors.OPERATION_ERROR);
        // 删除记录
        ThrowUtils.throwIf(!fileService.removeById(fileId), Errors.OPERATION_ERROR);
        return Result.success(true);
    }

    @GetMapping("/getGuideInfoList")
    @ApiOperation("教师获取指导学生信息")
    @SaCheckRole(UserConstant.TEACHER_ROLE)
    public Result getGuideInfoList(@RequestParam("current") Integer current,
                                   @RequestParam("size") Integer size) {
        ThrowUtils.throwIf(size > 100, Errors.PARAMS_ERROR);
        Page<TeacherGuideVo> guideVoPage = fileService.getGuideInfoList(current, size);
        return Result.success(guideVoPage);
    }
}
